<?php

namespace app\admin\controller;


use think\Controller;
use think\Db;
use think\Session;

class Index extends Authorized
{

    public function index()
    {

        return $this->fetch();
    }

    public function console()
    {
        $seoid=Session::get('admin.id');

        $gg=Db::table('tb_news')->limit(1)->order('sort desc,id desc')->select();
        $this->assign('gg', $gg);

//        我的钱包
        $myData=Db::table('tb_admin')->where(['id'=>$seoid])->find();
        $this->assign('myData', $myData);


//        数据
//      充值总金额：
        $dataAll_cz=Db::table('tb_money')->where(['typeid'=>0])->sum('money');
        $this->assign('dataAll_cz', $dataAll_cz);

////	消费总金额：
        $dataAll_xf=Db::table('tb_money')->where(['typeid'=>['<>',0]])->sum('money');
        $this->assign('dataAll_xf', $dataAll_xf);
//
////	    客户总数：
        $dataUser=Db::table('tb_user')->count();
        $this->assign('dataUser', $dataUser);
//
////	网址总数：
        $dataWeb=Db::table('tb_webinfo')->count();
        $this->assign('dataWeb', $dataWeb);
//
////    关键词总数：
        $dataWebkey=Db::table('tb_webkey')->count();
        $this->assign('dataWebkey', $dataWebkey);
//
////	扣费词总数（昨日）：
        $dataWebkey_kf=Db::table('tb_money')->where(['typeid'=>['>',0],'qfstate'=>0])->whereTime('intime', 'yesterday')->sum('money');
        $this->assign('dataWebkey_kf', $dataWebkey_kf);
//
////	当前月份销售总额：
        $dataMonth_kf=Db::table('tb_money')->where(['typeid'=>0])->whereTime('intime', 'month')->sum('money');
        $this->assign('dataMonth_kf', $dataMonth_kf);
//


//        扣费数据(不喊欠费)
//     总计：
        $kf_all=Db::table('tb_money')->where(['keyid'=>['>',0],'qfstate'=>0])->sum('money');
        $this->assign('kf_all', $kf_all);
//     昨日
        $kf_yesterday=Db::table('tb_money')->where(['keyid'=>['>',0],'qfstate'=>0])->whereTime('intime', 'yesterday')->sum('money');
        $this->assign('kf_yesterday', $kf_yesterday);
//     本周
        $kf_week=Db::table('tb_money')->where(['keyid'=>['>',0],'qfstate'=>0])->whereTime('intime', 'week')->sum('money');
        $this->assign('kf_week', $kf_week);
//     本月
        $kf_month=Db::table('tb_money')->where(['keyid'=>['>',0],'qfstate'=>0])->whereTime('intime', 'month')->sum('money');
        $this->assign('kf_month', $kf_month);
//     上月
        $kf_lastMonth=Db::table('tb_money')->where(['keyid'=>['>',0],'qfstate'=>0])->whereTime('intime', 'last month')->sum('money');
        $this->assign('kf_lastMonth', $kf_lastMonth);

//更新优化师数据
        $res=Db::table('tb_admin')->where(['state'=>0,'departID'=>3])->order('id desc')->select();
        foreach ($res as $k => $v) {
            $id=$v['id'];

//           优化师名下网站
            $webnum=Db::table('tb_webinfo')->where(['seoid'=>$id, 'state'=>1])->count();

//            优化师名下有排名的网站
            $weblist=Db::table('tb_webkey')->field('webID')->where(['seoid'=>$id,'state'=>1,'nowPm'=>['between',[1,20]]])->group('webID')->select();
            $webids=array();
            foreach ($weblist as $item){
                $webids[]=$item['webID'];
            }
            $webdbnum=Db::table('tb_webinfo')->where(['seoid'=>$id, 'state'=>1])->whereIn('id',$webids)->count();


//
////          优化师 名下有排名的 百度的词
            $keybdnum=Db::table('tb_webkey')->where(['seoid'=>$id, 'typeid'=>1,'state'=>1, 'nowPm'=>['between',[1,20]]])->whereTime('pmtime','today')->count();
////          优化师 名下 所有词
            $keynum=Db::table('tb_webkey')->where(['seoid'=>$id, 'typeid'=>1, 'state'=>1])->count();
            if($keynum>0 ){
//                更新优化师名下 关键词数量，百度关键词数量
                Db::table('tb_admin')->where(['id'=>$id])->update([
                    'webnum'=>$webnum,
                    'webdbnum'=>$webdbnum,
                    'keynum'=>$keynum,
                    'keybdnum'=>$keybdnum
                ]);
            }
        }


//优化排行榜
//        select  id,name,nickname,webnum,webdbnum,Convert(decimal(18,2),webdbnum*100.0/webnum) as data_web,keynum,keybdnum,Convert(decimal(18,2),keybdnum*100.0/keynum) as data_key from tb_admin where webnum>0 and webdbnum>0 order by data_key desc

        $list=Db::table('tb_admin')->field('id,name,nickname,webnum,webdbnum,Convert(decimal(18,2),webdbnum*100.0/webnum) as data_web,keynum,keybdnum,Convert(decimal(18,2),keybdnum*100.0/keynum) as data_key')
            ->where([
                'state'=>0,
                'departID'=>3,
                'keynum'=>['>',0],
                'webnum'=>['>',0]
            ])
            ->order('data_key desc')
            ->select();
        $this->assign('list', $list);




//     个人扣费数据(不喊欠费);
//     总计：
        $my_all=Db::table('tb_money')->where(['keyid'=>['>',0],'qfstate'=>0,'seoid'=>$seoid])->sum('money');
        $this->assign('my_all', $my_all);
//     昨日
        $my_yesterday=Db::table('tb_money')->where(['keyid'=>['>',0],'qfstate'=>0,'seoid'=>$seoid])->whereTime('intime', 'yesterday')->sum('money');
        $this->assign('my_yesterday', $my_yesterday);
//     本周
        $my_week=Db::table('tb_money')->where(['keyid'=>['>',0],'qfstate'=>0,'seoid'=>$seoid])->whereTime('intime', 'week')->sum('money');
        $this->assign('my_week', $my_week);
//     本月
        $my_month=Db::table('tb_money')->where(['keyid'=>['>',0],'qfstate'=>0,'seoid'=>$seoid])->whereTime('intime', 'month')->sum('money');
        $this->assign('my_month', $my_month);
//     上月
        $my_lastMonth=Db::table('tb_money')->where(['keyid'=>['>',0],'qfstate'=>0,'seoid'=>$seoid])->whereTime('intime', 'last month')->sum('money');
        $this->assign('my_lastMonth', $my_lastMonth);

        return $this->fetch();
    }


    public function change_pwd()
    {
        $data = $this->request->post();
        if($data){
            $res = Db::table('tb_admin')
                ->where([
                    'id' => Session::get('admin.id'),
                    'pwd'=>substr(md5($data['oldPassword']), 8, 16)
                ])->find();
            if ($res) {
                $result = Db::table('tb_admin')
                    ->where([
                        'id' => Session::get('admin.id')
                    ])
                    ->setField('pwd',substr(md5($data['password']), 8, 16));
                if ($result) {
                    return json(['msg' =>'修改成功', 'code' =>0]);
                }else{
                    return json(['msg' =>'修改失败', 'code' =>1]);
                }

            }else{
                return json(['msg' =>'旧密码错误', 'code' =>1]);
            }
        }else{
            return $this->fetch();
        }

    }


}